/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.ui.main;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.siro.pm.I18nManager;
import com.siro.pm.PmApplication;
import com.siro.pm.model.Developer;
import com.siro.pm.model.Document;
import com.siro.pm.model.Project;
import com.siro.pm.model.provider.DocumentProvider;
import com.siro.pm.ui.detail.DocumentDetailPage;
import com.siro.pm.utils.FileDownloadResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Link;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since DocumentsPage.java v1.0
 */
public class DocumentsPage extends VerticalLayout {
	private static final long serialVersionUID = -6963599324304409783L;
	private static final Logger logger = LoggerFactory
			.getLogger(DocumentsPage.class);

	DocumentsPage self;
	private Project project;
//	private List<Document> documents;

	private Button button_AddDocument;
	private Button button_DeleteDocument;
	public Table documentsTable;

	public DocumentsPage() {
		self = this;
	}

	public DocumentsPage(Project project, List<Document> documents) {
		this.project = project;
//		this.documents = documents;
	}

	public void createContents() {
		setSizeFull();
		setSpacing(true);
		setMargin(true, true, true, true);

		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);
		// Add Bug
		button_AddDocument = new Button(I18nManager.getMessage("button.add"));
		button_AddDocument.addListener(new ClickListener() {
			private static final long serialVersionUID = -5620810406204435724L;

			public void buttonClick(ClickEvent event) {
				DocumentDetailPage docDetailPage = new DocumentDetailPage();
				docDetailPage.setDocumentsPage(self);
				docDetailPage.setOwnerProject(project);
				docDetailPage.createContents();
				Window docDetailWindow = new Window(I18nManager
						.getMessage("document.info"));
				docDetailWindow.setModal(true);
				docDetailWindow.setWidth("800px");
				docDetailWindow.setHeight("80%");
				docDetailPage.setWindow(docDetailWindow);
				docDetailWindow.addComponent(docDetailPage);
				getWindow().addWindow(docDetailWindow);
			}
		});
		buttons.addComponent(button_AddDocument);
		buttons.setComponentAlignment(button_AddDocument, Alignment.TOP_RIGHT);

		// Delete Bug
		button_DeleteDocument = new Button(
				I18nManager.getMessage("button.delete"));
		if (!Developer.isProjectManager(project.getManager())) {
			button_DeleteDocument.setEnabled(false);
		}
		button_DeleteDocument.addListener(new ClickListener() {
			private static final long serialVersionUID = -5620810406204435724L;

			public void buttonClick(ClickEvent event) {
				Object object = documentsTable.getValue();
				if (object != null) {
					Document document = (Document) object;
					Document.deleteDocument(document);
					documentsTable.removeItem(document);
				}

			}
		});
		buttons.addComponent(button_DeleteDocument);
		buttons.setComponentAlignment(button_DeleteDocument,
				Alignment.TOP_RIGHT);

		addComponent(buttons);
		setComponentAlignment(buttons, Alignment.TOP_RIGHT);

		// Documents Table
		documentsTable = new Table();
		documentsTable.setWidth("100%");
		documentsTable.setHeight("100%");
		documentsTable.setSelectable(true);
		documentsTable.setColumnWidth(DocumentProvider.DOCUMENT_PROPERTY_NAME,
				300);
		documentsTable.setColumnWidth(
				DocumentProvider.DOCUMENT_PROPERTY_PROJECT, 200);
		documentsTable.setColumnExpandRatio(
				DocumentProvider.DOCUMENT_PROPERTY_PATH, 1);
		documentsTable.setContainerDataSource(DocumentProvider
				.getDocumentsData(project.getDocs()));
		documentsTable.setColumnReorderingAllowed(true);
		documentsTable.setColumnCollapsingAllowed(true);
		// Generate operations
		documentsTable.addGeneratedColumn(I18nManager.getMessage("operation"),
				new Table.ColumnGenerator() {
					private static final long serialVersionUID = -6031723727120033742L;

					public Component generateCell(Table source, Object itemId,
							Object columnId) {
						// Item item = projectsTable.getItem(itemId);
						HorizontalLayout opts = new HorizontalLayout();
						Document document = (Document) itemId;
						try {
							String filePath = "file:./" + document.getPath();
							filePath = new URL(filePath).getFile();
							File file = new File(filePath);
							FileDownloadResource imageResource = new FileDownloadResource(
									file, PmApplication.getDefault());
							Link download = new Link(I18nManager
									.getMessage("button.download"),
									imageResource);
							opts.addComponent(download);
						} catch (MalformedURLException e) {
							logger.error(e.getMessage(), e);
						}
						return opts;
					}

				});
		addComponent(documentsTable);
		setExpandRatio(documentsTable, 1);

	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

//	public List<Document> getDocuments() {
//		return documents;
//	}
//
//	public void setDocuments(List<Document> documents) {
//		this.documents = documents;
//	}

}